
import plugin from '../../../../lib/plugins/plugin.js';
import { getPlayerByE, pm } from './main.js';
import Rob from '../service/rob.js';
import Prop from '../model/item/prop.js';
import RobotError from '../model/errors/robot-error.js';
import ReviveMedicine from '../model/item/revive_medicine.js';


export class LNERobot extends plugin {
    playerManager: import("d:/QQRobot/Yunzai/Miao-Yunzai/plugins/LNERobot/ts/model/player/player_manager").default;
    constructor() {
        super({
            name: '我的物品',
            dsc: '机器人',
            event: 'message',
            priority: 5,
            rule: [
                {
                    /** 命令正则匹配 */
                    reg: '^我的物品|背包$',
                    /** 执行方法 */
                    fnc: 'myItem'
                },
                {
                    /** 命令正则匹配 */
                    reg: '^使用.*$',
                    /** 执行方法 */
                    fnc: 'useProp'
                },
                {
                    /** 命令正则匹配 */
                    reg: '^复活$',
                    /** 执行方法 */
                    fnc: 'reborn'
                },
            ]
        });

        this.playerManager = pm;
    }

    async reborn(e) {
        let player = await getPlayerByE(e);
        try {
            await super.reply(player.use(new ReviveMedicine).msg);
            await this.playerManager.savePlayer(player);
        } catch (err) {
            if (err instanceof RobotError) {
                await super.reply(err.message);
            }
        }
    }

    //TODO 
    async useProp(e) {
        let player = await getPlayerByE(e);
        let msg: string = e.msg;
        msg = msg.replace("使用", '');
        msg = msg.replace(" ", "");
        let prop = Prop.name2Class[msg];
        if (prop == undefined) {
            await super.reply("不存在这个东西");
            return;
        }
        try {
            await super.reply(player.use(prop).msg);
            await this.playerManager.savePlayer(player);
        } catch (err) {
            if (err instanceof RobotError) {
                await super.reply(err.message);
            }
        }
    }

    async myItem(e) {
        let player = await getPlayerByE(e);
        let items = player.items;
        let rMsg = "背包\n";
        items.forEach((count, item) => {
            rMsg += `- ${item.name} * ${count}\n`;
            rMsg += "  " + item.intro + '\n';
        });
        await super.reply(rMsg);
    }

    async rob(e) {
        if (isNaN(e.at)) {
            super.reply("请长按头像艾特，不能复制！");
            return;
        }

        let robber = await getPlayerByE(e);
        let target = await this.playerManager.getPlayerByQQNum(e.at);
        let rob = new Rob(robber, target);
        let result = await rob.do();
        if (!result.success) {
            await super.reply(result.msg);
            return;
        }


        let rpl = "";
        for (let i in result.records) {
            rpl += `第${Number.parseInt(i) + 1}回合\n${result.records[i].msg}\n\n`;
        }
        rpl += result.msg;

        await this.playerManager.savePlayer(robber);
        await this.playerManager.savePlayer(target);
        await super.reply(rpl);
    }

}
